package net.tngou.c4j.ik;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;











import net.tngou.c4j.CollectionGenerator;
import net.tngou.c4j.model.Url;
import net.tngou.c4j.thesaurus.parser.SogouScel;
import net.tngou.crawler.util.JsoupUtil;
import net.tngou.crawler.util.StringUtil;

import org.jsoup.nodes.Document;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.dic.Dictionary;




public class IKUtil {
	
	public static void main(String[] args) throws IOException {
		
		String url="https://www.women-health.cn/Health-knowledge/0129205U2018.html";
		Document doc = CollectionGenerator.get(url);
		List<WordFren> list = WordsFrem(doc.body().getElementsByClass("content").get(0).text(), Integer.MAX_VALUE);
		list.forEach(e->{System.err.println(e);});
//		System.err.println(doc);
//		System.err.println(Summarise(doc.body().getElementsByClass("content").get(0).text()));
	  
		
	}
	


	static Configuration cfg=null;
	static Dictionary dictionary =null;
	static Map<String, Integer> map=null;

	static{
		
		 cfg=  DefaultConfig.getInstance();
		 dictionary = Dictionary.initial(cfg);	
		 map= SogouScel.GetMap(); 
		 dictionary.addWords(map.keySet());
	

	
	}
	
	
	
	
	
	
	public static List<String> Title(String title,String input,int word,int size) {
		List<String> list = new ArrayList<String>();
		 input= JsoupUtil.Text(input);
		Map<String, Integer> sentenceCount =  new HashMap<String, Integer>();
		List<WordFren> words=  WordsFrem(title,word);
		String rep="(\\，|；|。|！|\\？)+(\\,|;|.|!|\\?)+(\\s|\\z)";
		String[] sts = StringUtil.split(input, rep);
		
		for (String sentence : sts) {
			
			words.forEach(e->{
				
				if(sentence.indexOf(e.getWord())>0)
				{
				 if(sentenceCount.containsKey(sentence)){
					 sentenceCount.put(sentence,sentenceCount.get(sentence)+1);
		         }else {
		        	 sentenceCount.put(sentence,1);
		         }
				}
			});
			
			
		}
		 List<Map.Entry<String, Integer>> sentenceList = new ArrayList<Map.Entry<String, Integer>>(sentenceCount.entrySet());
	     Collections.sort(sentenceList, ( a, b) ->  b.getValue().compareTo(a.getValue()));
	     for (int i = 0; i < sentenceList.size()&&i<size; i++) {
	    	 list.add(sentenceList.get(i).getKey().toString());
		}

		return list;
	}
	
	
	public static String Title(String title,String input) {
		List<String> list = Title(title, input, 100, 2);
		if(!list.isEmpty())
		{
			for (String string : list) {			
					if(string.length()<30 && string.length()>8)
					{
						title=string;
						break;
					}		
			}
		}
				
		return StringUtil.ReplaceBlank(title);
	}
	public static String Sentence(String input)
	{
		List<String> list =Summarise(input, 100, 5);
		String r="";
		for (String string : list) {
			r=r+string+" ";
		}
		return r;
	}
	
	public static List<String> Summarise(String input,int word,int size)  {
		
		List<String> list = new ArrayList<String>();
		 input= JsoupUtil.Text(input);
		Map<String, Integer> sentenceCount =  new HashMap<String, Integer>();
		List<WordFren> words=  WordsFrem( input,word);
		String rep="(\\。|！|\\？)+(\\.|!|\\?)+(\\s|\\z)";
		String[] sts = StringUtil.split(input, rep);
		
		for (String sentence : sts) {
			
			words.forEach(e->{
				
				if(sentence.indexOf(e.getWord())>0)
				{
				 if(sentenceCount.containsKey(sentence)){
					 sentenceCount.put(sentence,sentenceCount.get(sentence)+1);
		         }else {
		        	 sentenceCount.put(sentence,1);
		         }
				}
			});
			
			
		}
		 List<Map.Entry<String, Integer>> sentenceList = new ArrayList<Map.Entry<String, Integer>>(sentenceCount.entrySet());
	     Collections.sort(sentenceList, ( a, b) ->  b.getValue().compareTo(a.getValue()));
		
	     
	     for (int i = 0; i < sentenceList.size()&&i<size; i++) {
	    	 list.add(sentenceList.get(i).getKey().toString());
		}

		return list;
	}
	
	public static String Summarise(String input)  {
		String summarise="";
		List<String> list = Summarise(input, 100, 1);
		if(!list.isEmpty())summarise=list.get(0);
		return StringUtil.ReplaceBlank(summarise);
	}
	
	
	/**

     * 

    * @Title: getSentences

    * @Description: 把段落按. ! ?分隔成句组

    * @param @param input

    * @param @return    

    * @return String[]   

    * @throws

     */
//    public static String[] getSentences(String input) {

//        if (input == null) {

//            return new String[0];

//        } else {

//            // split on a ".", a "!", a "?" followed by a space or EOL

//        	//"(\\.|!|\\?)+(\\s|\\z)"

//            return input.split("(\\.|!|\\?)");

//        }

// 

//    }

	
	public static String  WordsFrem(String input) {
		String keyword="";
		 List<WordFren> list = WordsFrem(input,5);
		 for (WordFren wordFren : list) {
			 keyword=keyword+wordFren.getWord()+" ";
		}
		return keyword;
	}
	
	public static List<WordFren>  WordsFrem(String input,int size) {
		 List<WordFren> list = new ArrayList<WordFren>();
		 input= JsoupUtil.Text(input);
		Reader reader = new StringReader(input);
		IKSegmenter segmenter = new IKSegmenter(reader , true);
		Map<String, Integer> wordCount =  new HashMap<String, Integer>();

		Lexeme lexeme=null;  
        try {
			while((lexeme=segmenter.next())!=null){  
				
				 if(lexeme.getLexemeTypeString().startsWith("CN_WORD")){
//					 int frem = wordService.getWordFren(lexeme.getLexemeText());
//					 int frem =map.get(lexeme.getLexemeText())==null?0:map.get(lexeme.getLexemeText());
			         if(wordCount.containsKey(lexeme.getLexemeText())){

			        	 wordCount.put(lexeme.getLexemeText(),wordCount.get(lexeme.getLexemeText())+1);
			         }else {
			        	 wordCount.put(lexeme.getLexemeText(),1);
			         }
				 }
			}
		} catch (IOException e) {
			e.printStackTrace();
			return list;
		}
        List<Map.Entry<String, Integer>> wordFrenList = new ArrayList<Map.Entry<String, Integer>>(wordCount.entrySet());
        Collections.sort(wordFrenList, ( a, b) ->  b.getValue().compareTo(a.getValue()));
       
        
        for (int i = 0; i < wordFrenList.size()&&i<size; i++) {
        	WordFren fren = new WordFren();
        	Entry<String, Integer> wordFren = wordFrenList.get(i);
        	Integer value = wordFren.getValue();
        	String key = wordFren.getKey();
        	Integer frenint = map.get(key);
        	int filter=-1;
        	if(frenint==null)
        	{
        		frenint=0;
        		filter=0;
        	}
        	if(frenint>0)filter=1;
        	fren.setFilter(filter);
        	fren.setWord(key);
        	fren.setCount(value);
        	fren.setFren(frenint);
        	list.add(fren);
        	
		}
    
		return list;
		
	}
	
	
	
	
	 
	 private static boolean isCheck (List<Url> links,String word)
	 {
		 for (Url link : links) {
			 if(link.getText()!=null && link.getText().indexOf(word) >= 0)
					return true;
			}
		return false;
	 }
	 
	 
	

}